3252fb655fcaf32bdb147e225516a061d64be4a8
3 * See docs/deferred.txt
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @todo document (e.g. one-sentence top-level class description).
27 var $mId, //!< Page ID of the article linked from
28 $mTitle, //!< Title object of the article linked from
29 $mLinks, //!< Map of title strings to IDs for the links in the document
30 $mImages, //!< DB keys of the images used, in the array key only
31 $mTemplates, //!< Map of title strings to IDs for the template references, including broken ones
32 $mExternals, //!< URLs of external links, array key only
33 $mCategories, //!< Map of category names to sort keys
34 $mInterlangs, //!< Map of language codes to titles
35 $mProperties, //!< Map of arbitrary name to value
36 $mDb, //!< Database connection reference
37 $mOptions, //!< SELECT options to be used (array)
38 $mRecursive; //!< Whether to queue jobs for recursive updates
44 * @param $title Title of the page we're updating
45 * @param $parserOutput ParserOutput: output from a full parse of this page
46 * @param $recursive Boolean: queue jobs for recursive updates?
48 function __construct( $title, $parserOutput, $recursive = true ) {
49 global $wgAntiLockFlags;
51 if ( $wgAntiLockFlags & ALF_NO_LINK_LOCK
) {
52 $this->mOptions
= array();
54 $this->mOptions
= array( 'FOR UPDATE' );
56 $this->mDb
= wfGetDB( DB_MASTER
);
58 if ( !is_object( $title ) ) {
59 throw new MWException( "The calling convention to LinksUpdate::LinksUpdate() has changed. " .
60 "Please see Article::editUpdates() for an invocation example.\n" );
62 $this->mTitle
= $title;
63 $this->mId
= $title->getArticleID();
65 $this->mParserOutput
= $parserOutput;
66 $this->mLinks
= $parserOutput->getLinks();
67 $this->mImages
= $parserOutput->getImages();
68 $this->mTemplates
= $parserOutput->getTemplates();
69 $this->mExternals
= $parserOutput->getExternalLinks();
70 $this->mCategories
= $parserOutput->getCategories();
71 $this->mProperties
= $parserOutput->getProperties();
72 $this->mInterwikis
= $parserOutput->getInterwikiLinks();
74 # Convert the format of the interlanguage links
75 # I didn't want to change it in the ParserOutput, because that array is passed all
76 # the way back to the skin, so either a skin API break would be required, or an
77 # inefficient back-conversion.
78 $ill = $parserOutput->getLanguageLinks();
79 $this->mInterlangs
= array();
80 foreach ( $ill as $link ) {
81 list( $key, $title ) = explode( ':', $link, 2 );
82 $this->mInterlangs
[$key] = $title;
85 foreach ( $this->mCategories
as &$sortkey ) {
86 # If the sortkey is longer then 255 bytes,
87 # it truncated by DB, and then doesn't get
88 # matched when comparing existing vs current
89 # categories, causing bug 25254.
90 # Also. substr behaves weird when given "".
91 if ( $sortkey !== '' ) {
92 $sortkey = substr( $sortkey, 0, 255 );
96 $this->mRecursive
= $recursive;
98 wfRunHooks( 'LinksUpdateConstructed', array( &$this ) );
102 * Update link tables with outgoing links from an updated article
104 public function doUpdate() {
105 global $wgUseDumbLinkUpdate;
107 wfRunHooks( 'LinksUpdate', array( &$this ) );
108 if ( $wgUseDumbLinkUpdate ) {
109 $this->doDumbUpdate();
111 $this->doIncrementalUpdate();
113 wfRunHooks( 'LinksUpdateComplete', array( &$this ) );
116 protected function doIncrementalUpdate() {
117 wfProfileIn( __METHOD__
);
120 $existing = $this->getExistingLinks();
121 $this->incrTableUpdate( 'pagelinks', 'pl', $this->getLinkDeletions( $existing ),
122 $this->getLinkInsertions( $existing ) );
125 $existing = $this->getExistingImages();
127 $imageDeletes = $this->getImageDeletions( $existing );
128 $this->incrTableUpdate( 'imagelinks', 'il', $imageDeletes,
129 $this->getImageInsertions( $existing ) );
131 # Invalidate all image description pages which had links added or removed
132 $imageUpdates = $imageDeletes +
array_diff_key( $this->mImages
, $existing );
133 $this->invalidateImageDescriptions( $imageUpdates );
136 $existing = $this->getExistingExternals();
137 $this->incrTableUpdate( 'externallinks', 'el', $this->getExternalDeletions( $existing ),
138 $this->getExternalInsertions( $existing ) );
141 $existing = $this->getExistingInterlangs();
142 $this->incrTableUpdate( 'langlinks', 'll', $this->getInterlangDeletions( $existing ),
143 $this->getInterlangInsertions( $existing ) );
145 # Inline interwiki links
146 $existing = $this->getExistingInterwikis();
147 $this->incrTableUpdate( 'iwlinks', 'iwl', $this->getInterwikiDeletions( $existing ),
148 $this->getInterwikiInsertions( $existing ) );
151 $existing = $this->getExistingTemplates();
152 $this->incrTableUpdate( 'templatelinks', 'tl', $this->getTemplateDeletions( $existing ),
153 $this->getTemplateInsertions( $existing ) );
156 $existing = $this->getExistingCategories();
158 $categoryDeletes = $this->getCategoryDeletions( $existing );
160 $this->incrTableUpdate( 'categorylinks', 'cl', $categoryDeletes,
161 $this->getCategoryInsertions( $existing ) );
163 # Invalidate all categories which were added, deleted or changed (set symmetric difference)
164 $categoryInserts = array_diff_assoc( $this->mCategories
, $existing );
165 $categoryUpdates = $categoryInserts +
$categoryDeletes;
166 $this->invalidateCategories( $categoryUpdates );
167 $this->updateCategoryCounts( $categoryInserts, $categoryDeletes );
170 $existing = $this->getExistingProperties();
172 $propertiesDeletes = $this->getPropertyDeletions( $existing );
174 $this->incrTableUpdate( 'page_props', 'pp', $propertiesDeletes,
175 $this->getPropertyInsertions( $existing ) );
177 # Invalidate the necessary pages
178 $changed = $propertiesDeletes +
array_diff_assoc( $this->mProperties
, $existing );
179 $this->invalidateProperties( $changed );
181 # Refresh links of all pages including this page
182 # This will be in a separate transaction
183 if ( $this->mRecursive
) {
184 $this->queueRecursiveJobs();
187 wfProfileOut( __METHOD__
);
191 * Link update which clears the previous entries and inserts new ones
192 * May be slower or faster depending on level of lock contention and write speed of DB
193 * Also useful where link table corruption needs to be repaired, e.g. in refreshLinks.php
195 protected function doDumbUpdate() {
196 wfProfileIn( __METHOD__
);
198 # Refresh category pages and image description pages
199 $existing = $this->getExistingCategories();
200 $categoryInserts = array_diff_assoc( $this->mCategories
, $existing );
201 $categoryDeletes = array_diff_assoc( $existing, $this->mCategories
);
202 $categoryUpdates = $categoryInserts +
$categoryDeletes;
203 $existing = $this->getExistingImages();
204 $imageUpdates = array_diff_key( $existing, $this->mImages
) +
array_diff_key( $this->mImages
, $existing );
206 $this->dumbTableUpdate( 'pagelinks', $this->getLinkInsertions(), 'pl_from' );
207 $this->dumbTableUpdate( 'imagelinks', $this->getImageInsertions(), 'il_from' );
208 $this->dumbTableUpdate( 'categorylinks', $this->getCategoryInsertions(), 'cl_from' );
209 $this->dumbTableUpdate( 'templatelinks', $this->getTemplateInsertions(), 'tl_from' );
210 $this->dumbTableUpdate( 'externallinks', $this->getExternalInsertions(), 'el_from' );
211 $this->dumbTableUpdate( 'langlinks', $this->getInterlangInsertions(),'ll_from' );
212 $this->dumbTableUpdate( 'iwlinks', $this->getInterwikiInsertions(),'iwl_from' );
213 $this->dumbTableUpdate( 'page_props', $this->getPropertyInsertions(), 'pp_page' );
215 # Update the cache of all the category pages and image description
216 # pages which were changed, and fix the category table count
217 $this->invalidateCategories( $categoryUpdates );
218 $this->updateCategoryCounts( $categoryInserts, $categoryDeletes );
219 $this->invalidateImageDescriptions( $imageUpdates );
221 # Refresh links of all pages including this page
222 # This will be in a separate transaction
223 if ( $this->mRecursive
) {
224 $this->queueRecursiveJobs();
227 wfProfileOut( __METHOD__
);
230 function queueRecursiveJobs() {
231 global $wgUpdateRowsPerJob;
232 wfProfileIn( __METHOD__
);
234 $cache = $this->mTitle
->getBacklinkCache();
235 $batches = $cache->partition( 'templatelinks', $wgUpdateRowsPerJob );
237 wfProfileOut( __METHOD__
);
241 foreach ( $batches as $batch ) {
242 list( $start, $end ) = $batch;
244 'table' => 'templatelinks',
248 $jobs[] = new RefreshLinksJob2( $this->mTitle
, $params );
250 Job
::batchInsert( $jobs );
252 wfProfileOut( __METHOD__
);
256 * Invalidate the cache of a list of pages from a single namespace
258 * @param $namespace Integer
259 * @param $dbkeys Array
261 function invalidatePages( $namespace, $dbkeys ) {
262 if ( !count( $dbkeys ) ) {
267 * Determine which pages need to be updated
268 * This is necessary to prevent the job queue from smashing the DB with
269 * large numbers of concurrent invalidations of the same page
271 $now = $this->mDb
->timestamp();
273 $res = $this->mDb
->select( 'page', array( 'page_id' ),
275 'page_namespace' => $namespace,
276 'page_title IN (' . $this->mDb
->makeList( $dbkeys ) . ')',
277 'page_touched < ' . $this->mDb
->addQuotes( $now )
280 foreach ( $res as $row ) {
281 $ids[] = $row->page_id
;
283 if ( !count( $ids ) ) {
289 * We still need the page_touched condition, in case the row has changed since
290 * the non-locking select above.
292 $this->mDb
->update( 'page', array( 'page_touched' => $now ),
294 'page_id IN (' . $this->mDb
->makeList( $ids ) . ')',
295 'page_touched < ' . $this->mDb
->addQuotes( $now )
300 function invalidateCategories( $cats ) {
301 $this->invalidatePages( NS_CATEGORY
, array_keys( $cats ) );
305 * Update all the appropriate counts in the category table.
306 * @param $added associative array of category name => sort key
307 * @param $deleted associative array of category name => sort key
309 function updateCategoryCounts( $added, $deleted ) {
310 $a = new Article($this->mTitle
);
311 $a->updateCategoryCounts(
312 array_keys( $added ), array_keys( $deleted )
316 function invalidateImageDescriptions( $images ) {
317 $this->invalidatePages( NS_FILE
, array_keys( $images ) );
320 function dumbTableUpdate( $table, $insertions, $fromField ) {
321 $this->mDb
->delete( $table, array( $fromField => $this->mId
), __METHOD__
);
322 if ( count( $insertions ) ) {
323 # The link array was constructed without FOR UPDATE, so there may
324 # be collisions. This may cause minor link table inconsistencies,
325 # which is better than crippling the site with lock contention.
326 $this->mDb
->insert( $table, $insertions, __METHOD__
, array( 'IGNORE' ) );
331 * Update a table by doing a delete query then an insert query
334 function incrTableUpdate( $table, $prefix, $deletions, $insertions ) {
335 if ( $table == 'page_props' ) {
336 $fromField = 'pp_page';
338 $fromField = "{$prefix}_from";
340 $where = array( $fromField => $this->mId
);
341 if ( $table == 'pagelinks' ||
$table == 'templatelinks' ||
$table == 'iwlinks' ) {
342 if ( $table == 'iwlinks' ) {
343 $baseKey = 'iwl_prefix';
345 $baseKey = "{$prefix}_namespace";
347 $clause = $this->mDb
->makeWhereFrom2d( $deletions, $baseKey, "{$prefix}_title" );
354 if ( $table == 'langlinks' ) {
355 $toField = 'll_lang';
356 } elseif ( $table == 'page_props' ) {
357 $toField = 'pp_propname';
359 $toField = $prefix . '_to';
361 if ( count( $deletions ) ) {
362 $where[] = "$toField IN (" . $this->mDb
->makeList( array_keys( $deletions ) ) . ')';
368 $this->mDb
->delete( $table, $where, __METHOD__
);
370 if ( count( $insertions ) ) {
371 $this->mDb
->insert( $table, $insertions, __METHOD__
, 'IGNORE' );
377 * Get an array of pagelinks insertions for passing to the DB
378 * Skips the titles specified by the 2-D array $existing
381 function getLinkInsertions( $existing = array() ) {
383 foreach( $this->mLinks
as $ns => $dbkeys ) {
384 $diffs = isset( $existing[$ns] )
385 ?
array_diff_key( $dbkeys, $existing[$ns] )
387 foreach ( $diffs as $dbk => $id ) {
389 'pl_from' => $this->mId
,
390 'pl_namespace' => $ns,
399 * Get an array of template insertions. Like getLinkInsertions()
402 function getTemplateInsertions( $existing = array() ) {
404 foreach( $this->mTemplates
as $ns => $dbkeys ) {
405 $diffs = isset( $existing[$ns] ) ?
array_diff_key( $dbkeys, $existing[$ns] ) : $dbkeys;
406 foreach ( $diffs as $dbk => $id ) {
408 'tl_from' => $this->mId
,
409 'tl_namespace' => $ns,
418 * Get an array of image insertions
419 * Skips the names specified in $existing
422 function getImageInsertions( $existing = array() ) {
424 $diffs = array_diff_key( $this->mImages
, $existing );
425 foreach( $diffs as $iname => $dummy ) {
427 'il_from' => $this->mId
,
435 * Get an array of externallinks insertions. Skips the names specified in $existing
438 function getExternalInsertions( $existing = array() ) {
440 $diffs = array_diff_key( $this->mExternals
, $existing );
441 foreach( $diffs as $url => $dummy ) {
443 'el_from' => $this->mId
,
445 'el_index' => wfMakeUrlIndex( $url ),
452 * Get an array of category insertions
454 * @param $existing Array mapping existing category names to sort keys. If both
455 * match a link in $this, the link will be omitted from the output
458 function getCategoryInsertions( $existing = array() ) {
459 global $wgContLang, $wgCategoryCollation;
460 $diffs = array_diff_assoc( $this->mCategories
, $existing );
462 foreach ( $diffs as $name => $prefix ) {
463 $nt = Title
::makeTitleSafe( NS_CATEGORY
, $name );
464 $wgContLang->findVariantLink( $name, $nt, true );
466 if ( $this->mTitle
->getNamespace() == NS_CATEGORY
) {
468 } elseif ( $this->mTitle
->getNamespace() == NS_FILE
) {
474 # Treat custom sortkeys as a prefix, so that if multiple
475 # things are forced to sort as '*' or something, they'll
476 # sort properly in the category rather than in page_id
478 $sortkey = Collation
::singleton()->getSortKey(
479 $this->mTitle
->getCategorySortkey( $prefix ) );
482 'cl_from' => $this->mId
,
484 'cl_sortkey' => $sortkey,
485 'cl_timestamp' => $this->mDb
->timestamp(),
486 'cl_sortkey_prefix' => $prefix,
487 'cl_collation' => $wgCategoryCollation,
495 * Get an array of interlanguage link insertions
497 * @param $existing Array mapping existing language codes to titles
500 function getInterlangInsertions( $existing = array() ) {
501 $diffs = array_diff_assoc( $this->mInterlangs
, $existing );
503 foreach( $diffs as $lang => $title ) {
505 'll_from' => $this->mId
,
514 * Get an array of page property insertions
516 function getPropertyInsertions( $existing = array() ) {
517 $diffs = array_diff_assoc( $this->mProperties
, $existing );
519 foreach ( $diffs as $name => $value ) {
521 'pp_page' => $this->mId
,
522 'pp_propname' => $name,
523 'pp_value' => $value,
530 * Get an array of interwiki insertions for passing to the DB
531 * Skips the titles specified by the 2-D array $existing
534 function getInterwikiInsertions( $existing = array() ) {
536 foreach( $this->mInterwikis
as $prefix => $dbkeys ) {
537 $diffs = isset( $existing[$prefix] ) ?
array_diff_key( $dbkeys, $existing[$prefix] ) : $dbkeys;
538 foreach ( $diffs as $dbk => $id ) {
540 'iwl_from' => $this->mId
,
541 'iwl_prefix' => $prefix,
550 * Given an array of existing links, returns those links which are not in $this
551 * and thus should be deleted.
554 function getLinkDeletions( $existing ) {
556 foreach ( $existing as $ns => $dbkeys ) {
557 if ( isset( $this->mLinks
[$ns] ) ) {
558 $del[$ns] = array_diff_key( $existing[$ns], $this->mLinks
[$ns] );
560 $del[$ns] = $existing[$ns];
567 * Given an array of existing templates, returns those templates which are not in $this
568 * and thus should be deleted.
571 function getTemplateDeletions( $existing ) {
573 foreach ( $existing as $ns => $dbkeys ) {
574 if ( isset( $this->mTemplates
[$ns] ) ) {
575 $del[$ns] = array_diff_key( $existing[$ns], $this->mTemplates
[$ns] );
577 $del[$ns] = $existing[$ns];
584 * Given an array of existing images, returns those images which are not in $this
585 * and thus should be deleted.
588 function getImageDeletions( $existing ) {
589 return array_diff_key( $existing, $this->mImages
);
593 * Given an array of existing external links, returns those links which are not
594 * in $this and thus should be deleted.
597 function getExternalDeletions( $existing ) {
598 return array_diff_key( $existing, $this->mExternals
);
602 * Given an array of existing categories, returns those categories which are not in $this
603 * and thus should be deleted.
606 function getCategoryDeletions( $existing ) {
607 return array_diff_assoc( $existing, $this->mCategories
);
611 * Given an array of existing interlanguage links, returns those links which are not
612 * in $this and thus should be deleted.
615 function getInterlangDeletions( $existing ) {
616 return array_diff_assoc( $existing, $this->mInterlangs
);
620 * Get array of properties which should be deleted.
623 function getPropertyDeletions( $existing ) {
624 return array_diff_assoc( $existing, $this->mProperties
);
628 * Given an array of existing interwiki links, returns those links which are not in $this
629 * and thus should be deleted.
632 function getInterwikiDeletions( $existing ) {
634 foreach ( $existing as $prefix => $dbkeys ) {
635 if ( isset( $this->mInterwikis
[$prefix] ) ) {
636 $del[$prefix] = array_diff_key( $existing[$prefix], $this->mInterwikis
[$prefix] );
638 $del[$prefix] = $existing[$prefix];
645 * Get an array of existing links, as a 2-D array
648 function getExistingLinks() {
649 $res = $this->mDb
->select( 'pagelinks', array( 'pl_namespace', 'pl_title' ),
650 array( 'pl_from' => $this->mId
), __METHOD__
, $this->mOptions
);
652 foreach ( $res as $row ) {
653 if ( !isset( $arr[$row->pl_namespace
] ) ) {
654 $arr[$row->pl_namespace
] = array();
656 $arr[$row->pl_namespace
][$row->pl_title
] = 1;
662 * Get an array of existing templates, as a 2-D array
665 function getExistingTemplates() {
666 $res = $this->mDb
->select( 'templatelinks', array( 'tl_namespace', 'tl_title' ),
667 array( 'tl_from' => $this->mId
), __METHOD__
, $this->mOptions
);
669 foreach ( $res as $row ) {
670 if ( !isset( $arr[$row->tl_namespace
] ) ) {
671 $arr[$row->tl_namespace
] = array();
673 $arr[$row->tl_namespace
][$row->tl_title
] = 1;
679 * Get an array of existing images, image names in the keys
682 function getExistingImages() {
683 $res = $this->mDb
->select( 'imagelinks', array( 'il_to' ),
684 array( 'il_from' => $this->mId
), __METHOD__
, $this->mOptions
);
686 foreach ( $res as $row ) {
687 $arr[$row->il_to
] = 1;
693 * Get an array of existing external links, URLs in the keys
696 function getExistingExternals() {
697 $res = $this->mDb
->select( 'externallinks', array( 'el_to' ),
698 array( 'el_from' => $this->mId
), __METHOD__
, $this->mOptions
);
700 foreach ( $res as $row ) {
701 $arr[$row->el_to
] = 1;
707 * Get an array of existing categories, with the name in the key and sort key in the value.
710 function getExistingCategories() {
711 $res = $this->mDb
->select( 'categorylinks', array( 'cl_to', 'cl_sortkey_prefix' ),
712 array( 'cl_from' => $this->mId
), __METHOD__
, $this->mOptions
);
714 foreach ( $res as $row ) {
715 $arr[$row->cl_to
] = $row->cl_sortkey_prefix
;
721 * Get an array of existing interlanguage links, with the language code in the key and the
722 * title in the value.
725 function getExistingInterlangs() {
726 $res = $this->mDb
->select( 'langlinks', array( 'll_lang', 'll_title' ),
727 array( 'll_from' => $this->mId
), __METHOD__
, $this->mOptions
);
729 foreach ( $res as $row ) {
730 $arr[$row->ll_lang
] = $row->ll_title
;
736 * Get an array of existing inline interwiki links, as a 2-D array
737 * @return array (prefix => array(dbkey => 1))
739 protected function getExistingInterwikis() {
740 $res = $this->mDb
->select( 'iwlinks', array( 'iwl_prefix', 'iwl_title' ),
741 array( 'iwl_from' => $this->mId
), __METHOD__
, $this->mOptions
);
743 foreach ( $res as $row ) {
744 if ( !isset( $arr[$row->iwl_prefix
] ) ) {
745 $arr[$row->iwl_prefix
] = array();
747 $arr[$row->iwl_prefix
][$row->iwl_title
] = 1;
753 * Get an array of existing categories, with the name in the key and sort key in the value.
756 function getExistingProperties() {
757 $res = $this->mDb
->select( 'page_props', array( 'pp_propname', 'pp_value' ),
758 array( 'pp_page' => $this->mId
), __METHOD__
, $this->mOptions
);
760 foreach ( $res as $row ) {
761 $arr[$row->pp_propname
] = $row->pp_value
;
768 * Return the title object of the page being updated
770 function getTitle() {
771 return $this->mTitle
;
775 * Return the list of images used as generated by the parser
777 public function getImages() {
778 return $this->mImages
;
782 * Invalidate any necessary link lists related to page property changes
784 function invalidateProperties( $changed ) {
785 global $wgPagePropLinkInvalidations;
787 foreach ( $changed as $name => $value ) {
788 if ( isset( $wgPagePropLinkInvalidations[$name] ) ) {
789 $inv = $wgPagePropLinkInvalidations[$name];
790 if ( !is_array( $inv ) ) {
791 $inv = array( $inv );
793 foreach ( $inv as $table ) {
794 $update = new HTMLCacheUpdate( $this->mTitle
, $table );